package net.zhengxinyun.performance.controller.exam;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.OptionClasEntity;
import net.zhengxinyun.performance.entity.SelectIonEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.OptionClasFilter;
import net.zhengxinyun.performance.filter.SelectionFilter;
import net.zhengxinyun.performance.service.exam.select.OptionClasService;
import net.zhengxinyun.performance.service.exam.select.SelectionService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * ----问卷选项---
 *
 */
@RestController
@RequestMapping("/option")
public class OptionCtrl {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private OptionClasService clasService;

    @Autowired
    private SelectionService selectionService;

    /**
     *  ----------------大类选项----------
     * @param param
     * @return
     */
    @PostMapping("/class/query")
    public Result<HashMap<String,Object>> query(@RequestBody OptionParam param)
    {

        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            HashMap<String,Object> map = new HashMap<>();

            List<OptionClasFilter> query = clasService.query(param.filter);
            if(query.size()==0)
            {
                param.filter.setPageNumber(param.filter.getPageNumber()-1);
                query =clasService.query(param.filter);
            }
            int total = clasService.queryForCount(param.filter);

            SelectionFilter sefilter = new SelectionFilter();
            for (OptionClasFilter item : query) {
                sefilter.setOptionId(item.getId());

                List<SelectionFilter> list = selectionService.query(sefilter);
                item.setSelectList(list);
            }

            map.put("list",query);
            map.put("pageTotal",total);

            return ResultUtils.getSuccessResults(map);
        }
        return  ResultUtils.getResults(Result.Status.OVERTIME,"error token",null);
    }

    @PostMapping("/class/insert")
    public  Result<String> insertExamIndex(@RequestBody OptionParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            param.entity.setOpValue(0);
            param.entity.setId(null);
            int insert = clasService.insert(param.entity);
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"添加失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/class/update")
    public  Result<String> updatetExamIndex(@RequestBody OptionParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            int insert = clasService.updateByPrimaryKey(param.entity);
            if(insert ==0){
                return ResultUtils.getResults(Result.Status.ERROR,"更新失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/class/delete")
    public  Result<String> deletetExamIndex(@RequestBody OptionParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            int insert = clasService.deleteByPrimaryKey(param.entity.getId());
            if (insert == 0) {
                return ResultUtils.getResults(Result.Status.ERROR, "删除失败");
            }
            return ResultUtils.getResults(Result.Status.SUCCESS, "操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }


    /**
     * ----------小类选项--------  由大类确定小类
     */

    @PostMapping("/select/query")
    public Result<HashMap<String,Object>> selectQuery(@RequestBody OptionParam param)
    {

        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            HashMap<String,Object> map = new HashMap<>();

            List<SelectionFilter> query = selectionService.query(param.selectFilter);
            if(query.size()==0)
            {
                param.selectFilter.setPageNumber(param.selectFilter.getPageNumber()-1);
                query =selectionService.query (param.selectFilter);
            }
            int total = selectionService.queryForCount(param.selectFilter);
            map.put("list",query);
            map.put("pageTotal",total);

            return ResultUtils.getSuccessResults(map);
        }
        return  ResultUtils.getResults(Result.Status.OVERTIME,"error token",null);
    }

    @PostMapping("/select/insert")
    public  Result<String> insertSelect(@RequestBody OptionParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            List<SelectIonEntity> selectList = param.selectEntity.getSelectList();

            if(ObjectUtils.isBlank(selectList)|| selectList.size()==0)
            {
                return  ResultUtils.getResults(Result.Status.ERROR,"无数据添加");
            }

            for (SelectIonEntity item : selectList) {
                item.setOptionId(param.selectEntity.getOptionId());
                item.setDel(0);
                int insert = selectionService.insert(item);
                if(insert ==0){
                    return ResultUtils.getResults(Result.Status.ERROR,"添加失败");
                }

            }

            // 设置为有选项值的 选项
            OptionClasEntity opClas = clasService.selectByPrimaryKey(param.selectEntity.getOptionId());
            opClas.setOpValue(1);
            clasService.updateByPrimaryKey(opClas);

            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/select/update")
    public  Result<String> updatetSelect(@RequestBody OptionParam param)
    {
        if(tokenService.compareToken(param.tokenEntity.getValue()))
        {
            List<SelectIonEntity> selectList = param.selectEntity.getSelectList();

            if(ObjectUtils.isBlank(selectList)|| selectList.size()==0)
            {
                return  ResultUtils.getResults(Result.Status.ERROR,"无数据");
            }
             //先删除 原来的。
            Long opId = param.selectEntity.getOptionId();
            if(ObjectUtils.isBlank(opId))
            {
                return  ResultUtils.getResults(Result.Status.ERROR,"无数据");
            }
            selectionService.deleteByPrimaryKey(opId);
            for (SelectIonEntity item : selectList) {
                item.setDel(0);
                item.setOptionId(param.selectEntity.getOptionId());

                int insert = selectionService.insert(item);
                if(insert ==0){
                    return ResultUtils.getResults(Result.Status.ERROR,"添加失败");
                }

            }

            /*for (SelectIonEntity item : selectList) {

                int insert = selectionService.updateByPrimaryKey(item);
                if(insert ==0){
                    return ResultUtils.getResults(Result.Status.ERROR,"更新失败");
                }

            }*/



            return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }

    @PostMapping("/select/delete")
    public  Result<String> deletetSelect(@RequestBody OptionParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            Long opId = param.entity.getId();
            int  del =selectionService.deleteByPrimaryKey(opId);
           // int insert = selectionService.deleteByPrimaryKey(param.selectEntity.getId());
            if (del == 0) {
                return ResultUtils.getResults(Result.Status.ERROR, "删除失败");
            }



            return ResultUtils.getResults(Result.Status.SUCCESS, "操作成功");
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "登录过期");
    }








}

class OptionParam {
    public OptionClasFilter filter;
    public OptionClasEntity entity;
    public SelectIonEntity  selectEntity;
    public SelectionFilter  selectFilter;

    public TokenEntity tokenEntity;
}